package com.middlegroup.common.utils;
 
import org.apache.commons.lang3.StringUtils;

/**掩码工具类
 * @author pc
 */
public class MaskUtils {

	/**姓名掩码
	 * @param name
	 * @return
	 */
	public static String maskName(String name){
		if (StringUtils.isBlank(name)){
			return name;
		}
		name = name.trim();
		int length = name.length();
		String firstOneNo = name.substring(0, 1);
		//String lastFourNo = name.substring(length - 4);
		String mask = "";
		for (int i = 0; i < length ; i++) {
			mask = mask + "*";
		}
		return firstOneNo + mask ;
	}

	public static String maskCellphone(String cellphoneNo) {
		if ((cellphoneNo == null) || (cellphoneNo.trim().length() != 11)) {
			return cellphoneNo;
		}
		return cellphoneNo.substring(0, 3) + "****" + cellphoneNo.substring(cellphoneNo.length() - 4);
	}

	/**邮箱掩码
	 * @param email
	 * @return
	 */
	public static String maskEmail(String email) {
		if (StringUtils.isBlank(email)) {
			return "";
		}
		int index = StringUtils.indexOf(email, "@");
		if (index <= 1){
			return email;
		}else{
			return StringUtils.rightPad(StringUtils.left(email, 1), index, "*")
					.concat(StringUtils.mid(email, index, StringUtils.length(email)));
		}
	}


	private static String maskCardNo(String cardNo) {
		if ((cardNo == null) || (cardNo.trim().length() <= 8)) {
			return cardNo;
		}
		cardNo = cardNo.trim();
		int length = cardNo.length();
		String firstFourNo = cardNo.substring(0, 4);
		String lastFourNo = cardNo.substring(length - 4);
		String mask = "";
		for (int i = 0; i < length - 8; i++) {
			mask = mask + "*";
		}
		return firstFourNo + mask + lastFourNo;
	}

	/**身份证掩码
	 * @param idCardNo
	 * @return
	 */
	public static String maskIDCardNo(String idCardNo) {
		return maskCardNo(idCardNo);
	}

	/**银行卡掩码
	 * @param bankCardNo
	 * @return
	 */
	public static String maskBankCardNo(String bankCardNo) {
		return maskCardNo(bankCardNo);
	}

	/**根据aesKey加密敏感字段
	 * @param s
	 * @param aesKey
	 * @return
	 */
	public static String encryptByKey(String s,String aesKey)  {
		if (StringUtils.isBlank(s)){
			return s;
		}
		s = AesUtils.encrypt(s,aesKey);
		return s;
	}
	
}